package com.chainyoung.component_tmperson.ui.activity.debit;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.common_widget.loadsir.callback.Callback;
import com.chainyoung.common.common_widget.loadsir.core.LoadService;
import com.chainyoung.common.common_widget.loadsir.core.LoadSir;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.custom_widget.EmptyCallback;
import com.chainyoung.common.custom_widget.LoadingCallback;
import com.chainyoung.common.custom_widget.RetryCallback;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.di.component.DaggerPersonActivityComponent;
import com.chainyoung.component_tmperson.di.module.PersonActivityModule;
import com.chainyoung.component_tmperson.entity.OwnWalletDebit;
import com.chainyoung.component_tmperson.entity.OwnWalletDebitItem;
import com.chainyoung.component_tmperson.entity.WalletDebit;
import com.chainyoung.component_tmperson.ui.adapter.OwnWalletDebitAdapter;
import com.chainyoung.component_tmperson.ui.mvp.contract.OwnWalletDebitContract;
import com.chainyoung.component_tmperson.ui.mvp.presenter.OwnWalletDebitPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 个人已贷
 */
public class WalletDebitActivity extends BaseActivity<OwnWalletDebitPresenter> implements OwnWalletDebitContract.View {

    @BindView(R2.id.iv_back)
    ImageView ivBack;
    @BindView(R2.id.tv_coin_type)
    TextView tvCoinType;
    @BindView(R2.id.iv_eye)
    ImageView ivEye;
    @BindView(R2.id.tv_available_debit_amount)
    TextView tvAvailableDebitAmount;
    @BindView(R2.id.txt_repayment)
    TextView txtRepayment;
    @BindView(R2.id.tv_repayment_info)
    TextView tvRepaymentInfo;
    @BindView(R2.id.ll_top_bottom)
    RelativeLayout llTopBottom;
    @BindView(R2.id.ll_finacing_profit_top)
    LinearLayout llFinacingProfitTop;
    @BindView(R2.id.txt_mark_coin_type)
    TextView txtMarkCoinType;
    @BindView(R2.id.rv_list)
    RecyclerView rvList;
    @BindView(R2.id.refresh_layout)
    SmartRefreshLayout refreshLayout;

    private WalletDebit walletDebit;

    private List<OwnWalletDebitItem> mData = new ArrayList<>();

    private OwnWalletDebitAdapter adapter;

    private LoadService loadService;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPersonActivityComponent.builder()
                .appComponent(appComponent)
                .personActivityModule(new PersonActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

    @Override
    protected void initUI() {
        setCurrentTitle("我的贷币");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(ContextCompat.getColor(mContext, R.color._008127));
        }
        walletDebit = (WalletDebit) getIntent().getSerializableExtra("walletDebit");
        if (TextUtils.isEmpty(walletDebit.getNext_repay_date())) {
            llTopBottom.setVisibility(View.INVISIBLE);
        }
        tvCoinType.setText("可借额度(" + walletDebit.getCoin().toUpperCase() + ")");
        tvRepaymentInfo.setText(walletDebit.getNext_repay_date() + "日自动还款" + StringUtil.subZeroAndDot(walletDebit.getNext_repay_amount()));
        initLoadSir();
        initRecyclerView();
    }

    private void initLoadSir() {
        LoadSir loadSir = new LoadSir.Builder()
                .addCallback(new LoadingCallback())
                .addCallback(new EmptyCallback())
                .addCallback(new RetryCallback())
                .setDefaultCallback(LoadingCallback.class)
                .build();
        loadService = loadSir.register(refreshLayout, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                getOwnDebitList();
            }
        });
    }

    private void initRecyclerView() {
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new OwnWalletDebitAdapter(mData, walletDebit);
        rvList.setAdapter(adapter);
        DividerItemDecoration itemDecoration = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.shape_divider_gray_10));
        rvList.addItemDecoration(itemDecoration);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getOwnDebitList();
            }
        });
    }

    @Override
    protected void initData() {
    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_activity_wallet_debit;
    }


    @OnClick(R2.id.iv_back)
    void back() {
        finish();
    }

    @OnClick(R2.id.tv_goto_debit)
    void goToDebit() {
        Intent intent = new Intent(mContext, WalletApplyDebitActivity.class);
        intent.putExtra("walletDebit", walletDebit);
        //intent.putExtra("isFromOwn",true);
        mContext.startActivity(intent);
    }


    private void getAvaliableAmount() {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put(LibConstant.TOKEN, SPUtil.getString(LibConstant.TOKEN));
        map.put("coin", walletDebit.getCoin());
        mPresenter.getDebitCredit(map);
    }

    private void getOwnDebitList() {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put(LibConstant.TOKEN, SPUtil.getString(LibConstant.TOKEN));
        map.put("coin", walletDebit.getCoin());
        mPresenter.getOwnWalletDebit(map);
    }


    @Override
    public void onGetOwnWalletDebitSuccessed(OwnWalletDebit data) {
        refreshLayout.finishRefresh();
        mData.clear();
        if (data != null && !data.getList().isEmpty()) {
            mData.addAll(data.getList());
        }
        if (data != null && data.getNext_repay() != null) {
            llTopBottom.setVisibility(TextUtils.isEmpty(data.getNext_repay().getRepay_day()) ? View.INVISIBLE : View.VISIBLE);
            tvRepaymentInfo.setText(data.getNext_repay().getRepay_day() + "日自动还款" + StringUtil.subZeroAndDot(data.getNext_repay().getRepay_amount()));
        }
        adapter.notifyDataSetChanged();
        if (mData.isEmpty()) {
            loadService.showCallback(EmptyCallback.class);
        } else {
            loadService.showSuccess();

        }
    }

    @Override
    public void onGetOwnWalletDebitFailed() {
        refreshLayout.finishRefresh();
        loadService.showCallback(RetryCallback.class);
    }

    @Override
    public void onGetDebitCredit(String data) {
        tvAvailableDebitAmount.setText(StringUtil.subZeroAndDot(data));
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAvaliableAmount();
        getOwnDebitList();
    }
}
